<% this.title = '排名 - ' + contest.title %>
<% include header %>
<style>
.submit_time {
  font-size: 0.8em;
  margin-top: 5px;
  color: #000;
}
</style>
<div class="padding">
<h1 class="ui center aligned header" style="margin-bottom: 30px; "><%= contest.title %></h1>
        <table class="ui very basic center aligned table">
            <thead>
            <tr>
                <th>#</th>
                <th>用户名</th>
                <% if (contest.type === 'acm') { %>
                  <th>通过数量</th>
                  <th>罚时</th>
                <% } %>
                <% for (let i = 0; i < problems.length; i++) { %>
                <th>
                  <a href="<%= syzoj.utils.makeUrl(['contest', contest.id, 'problem', i + 1]) %>">
                    <%= syzoj.utils.removeTitleTag(problems[i].title) %>
                  </a>
                </th>
                <% } %>
                <% if (contest.type === 'noi' || contest.type === 'ioi') { %>
                  <th>总分</th>
                <% } %>
            </tr>
            </thead>
            <tbody>
            <%
            for (let problem of problems) {
              let i = 0, min, minPos = -1;
              for (let item of ranklist) {
                i++;
                let condition;
                if (contest.type === 'acm') condition = item.player.score_details[problem.id] && item.player.score_details[problem.id].accepted && (minPos === -1 || item.player.score_details[problem.id].acceptedTime < min.player.score_details[problem.id].acceptedTime);
                else condition = item.player.score_details[problem.id] && item.player.score_details[problem.id].score === 100 && (minPos === -1 || item.player.score_details[problem.id].judge_state.submit_time < min.player.score_details[problem.id].judge_state.submit_time);
                if (condition) {
                  min = item;
                  minPos = i;
                }
              }
              problem.min = minPos;
            }

            let i = 0, rank = 0, lastItem;
            for (let item of ranklist) {
              i++;
              let latest = contest.start_time, timeSum = 0, unacceptedCount = 0;
            %>
            <tr>
                <%
                  if (contest.type === 'noi' || contest.type === 'ioi') {
                    if (i === 1 || item.player.score !== lastItem.player.score) rank = i;
                  } else if (contest.type === 'acm') {
                    for (let problem of problems) {
                      if (item.player.score_details[problem.id] && item.player.score_details[problem.id].accepted) {
                        timeSum += (item.player.score_details[problem.id].acceptedTime - contest.start_time) + (item.player.score_details[problem.id].unacceptedCount * 20 * 60);
                        unacceptedCount += item.player.score_details[problem.id].unacceptedCount;
                      }
                    }
                    item.player.timeSum = timeSum;

                    if (i === 1 || item.player.score !== lastItem.player.score || item.player.timeSum !== lastItem.player.timeSum) rank = i;
                  }
                %>
                <td>
                  <% if (rank == 1) { %>
                    <div class="ui yellow ribbon label">
                  <% } else if (rank == 2) { %>
                    <div class="ui ribbon label">
                  <% } else if (rank == 3) { %>
                    <div class="ui brown ribbon label" style="background-color: #C47222 !important;">
                  <% } else { %>
                    <div>
                  <% } %>
                  <%= rank %>
                  </div>
                </td>
                <td><a href="<%= syzoj.utils.makeUrl(['user', item.user.id]) %>"><%= item.user.username %></a><% if (item.user.nameplate) { %><%- item.user.nameplate %><% } %></td>
                <% if (contest.type === 'acm') { %>
                  <td>
                    <span class="score score_<%= parseInt((item.player.score / ranklist[0].player.score * 10) || 0) %>">
                      <%= item.player.score %>
                    </span>
                  </td>
                  <td>
                    <%= syzoj.utils.formatTime(timeSum) %>
                  </td>
                <% } %>
                <% for (let problem of problems) { %>
                  <% if (problem.min === i) { %>
                    <td style="background: rgb(244, 255, 245); ">
                  <% } else { %>
                    <td>
                  <% } %>
                  <% if (!item.player.score_details[problem.id]) { %>
                    </td>
                  <% } else if (contest.type === 'acm') { %>
                      <a href="<%= syzoj.utils.makeUrl(['submission', item.player.score_details[problem.id].judge_id]) %>">
                        <% if (item.player.score_details[problem.id].accepted) { %>
                          <span class="score score_10">
                            <% if (item.player.score_details[problem.id].unacceptedCount) { %>
                              +<%= item.player.score_details[problem.id].unacceptedCount %>
                            <% } else { %>
                              +
                            <% } %>
                          </span>

                          <div class="submit_time">
                            <%= syzoj.utils.formatTime(item.player.score_details[problem.id].acceptedTime - contest.start_time) %>
                          </div>
                        <% } else if (item.player.score_details[problem.id].unacceptedCount) { %>
                          <span class="score score_0">
                            -<%= item.player.score_details[problem.id].unacceptedCount %>
                          </span>
                        <% } %>
                      </a>
                    </td>
                  <% } else if (contest.type === 'noi' || contest.type === 'ioi') { %>
                      <a href="<%= syzoj.utils.makeUrl(['submission', item.player.score_details[problem.id].judge_id]) %>">
                        <% if (item.player.score_details[problem.id].weighted_score != null) { %>
                          <span class="score score_<%= parseInt((item.player.score_details[problem.id].score / 10) || 0) %>">
                            <%= Math.round(item.player.score_details[problem.id].weighted_score) %>
                          </span>
                        <% } else { %>
                          <span class="status compile_error">
                            0
                          </span>
                        <% } %>
                      </a>
                      <div class="submit_time">
                        <%= syzoj.utils.formatTime(item.player.score_details[problem.id].judge_state.submit_time - contest.start_time) %>
                        <% latest = Math.max(latest, item.player.score_details[problem.id].judge_state.submit_time)  %>
                      </div>
                    </td>
                  <% } %>
                <% } %>
                <% if (contest.type === 'noi' || contest.type === 'ioi') { %>
                  <td>
                    <span class="score score_<%= parseInt((item.player.score / ranklist[0].player.score * 10) || 0) %>">
                      <%= item.player.score %>
                    </span>
                    <div class="submit_time">
                      <%= syzoj.utils.formatTime(latest - contest.start_time) %>
                    </div>
                  </td>
                <% } %>
            </tr>
            <% lastItem = item; %>
            <% } %>
            </tbody>
        </table>
<% if (!ranklist.length) { %>
<div style="background-color: #fff; height: 18px; margin-top: -18px; "></div>
<div class="ui placeholder segment" style="margin-top: 0px; ">
  <div class="ui icon header">
    <i class="ui file icon" style="margin-bottom: 20px; "></i>
    暂无选手提交
  </div>
</div>
<% } %>
</div>
<% include footer %>
